<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>举报</title>
    <link rel="stylesheet" href="/css/bootstrap.css">
    <script src="/js/jquery.js"></script>
    <script src="/js/distpicker.js"></script>
    <style>
        .top-buffer1{
            margin-top: 20px;
        }
        .top-buffer2{
            margin-top: 10px;
        }
        body{

        }
    </style>
<body>
<div class="panel panel-success">
    <div class="panel-heading">
        <h3 class="panel-title top-buffer1" style="text-align: center">举报</h3>
    </div>
    <form  class="row g-3" id="accuse_form" enctype="multipart/form-data" style="border: none">
        <!---隐藏的用户名-->
        <div class="row top-buffer1">
            <div class="col-md-4"></div>
            <div class="col-md-4">
                <label for="accuse_type" class="form-label">举报类型</label>
                <select id="accuse_type" class="form-select" name="accusetype">
                    <option  value="用户不良行为">用户不良行为</option>
                    <option  value="房屋描述不符">房屋描述不符</option>
                    <option  value="合租招募信息不实">合租招募信息不实</option>
                    <option  value="诈骗行为">诈骗行为</option>
                    <option  value="其他">其他</option>
                </select>
            </div>
        </div>

        <input type="text" th:value="${session.user.userid}" name="userid" style="display: none">
        <input type="text" th:value="${session.beAccusedId}" name="beAccusedId" style="display: none">

        <div class="row top-buffer1">
            <div class="col-md-4"></div>
            <div class="col-md-4">
                <label for="accuse_description">详细举报内容描述</label>
                <textarea class="form-control top-buffer2" rows="3" placeholder="请输入内容" id="accuse_description" name="accusedescription"></textarea>
            </div>
        </div>
        <div class="row top-buffer1">
            <div class="col-md-4"></div>
            <div class="col-md-4 file">
                <label for="accuse_image">举报图片证据:</label>
                <input type="file" name="accuseimage"  class="form-control top-buffer2" id="accuse_image" multiple>
            </div>
        </div>
        <!--<div class="row top-buffer2">
            <div class="col-md-4"></div>
            <div class="col-md-4 content-img">
                <ul class="container content-img-list">
                    <li class="content-img-list-item"><img id="img1" class="img-thumbnail rounded" width="300" height="200" alt="上传的图片" /><a class="delete-btn"><i class="ico-delete"></i></a></li>
                </ul>
            </div>
        </div>-->
        <div class="row top-buffer2">
            <div class="col-md-4"></div>
            <div class="col-md-4" id="imageContainer"></div>
        </div>
        <div style="text-align: center">
            <a class="btn btn-outline-dark" style="width: 20%;" onclick="addAccuse()">提交</a>
        </div>
    </form>
</div>
<script>
    var fileInput = document.getElementById("accuse_image");
    fileInput.addEventListener('change', function (event) {
        var input = event.target;
        var fileList = input.files;
        var imgName = new Array();
        var imgSrc = new Array();
        var imgFile = new Array();
        for(var i = 0; i < input.files.length; i++){
            var imgSrcI = getObjectURL(fileList[i]);
            imgName.push(fileList[i].name);
            imgSrc.push(imgSrcI);
            imgFile.push(fileList[i]);
        }
        for(var a = 0; a < imgSrc.length; a++){
            var img = document.createElement("img");
            img.setAttribute("id", "img" + a);
            img.src = imgSrc[a];
            var div1 = document.getElementById("imageContainer");
            img.style = "width:100px; height:100px";
            img.className = "img-thumbnail";
            div1.appendChild(img);
        }
    })


    function getObjectURL(file) {
        var url = null ;
        if (window.createObjectURL!=undefined) { // basic
            url = window.createObjectURL(file) ;
        } else if (window.URL!=undefined) { // mozilla(firefox)
            url = window.URL.createObjectURL(file) ;
        } else if (window.webkitURL!=undefined) { // webkit or chrome
            url = window.webkitURL.createObjectURL(file) ;
        }
        return url ;
    }
    function addAccuse() {
        var formData = new FormData($("#accuse_form")[0]);
        for (var i = 0; i <$("#accuse_image")[0].files.length;i++){
            formData.append("file", $("#accuse_image")[0].files[i]);
        }
        $.ajax({
            url: "/accuse/addAccuse",
            type: 'POST',
            data: formData,
            processData: false,
            contentType: false,
            success: function () {
                alert("success!")
            },
            error: function (req, status, error) {
                alert("请求处理出错!!!" + error);
            }
        });
    }
</script>
</body>
</html>